<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="厂区" prop="factoryId">
        <el-select v-model="queryParams.factoryId" placeholder="请选择归属厂区" filterable>
          <el-option v-for="(item,index) in factoryList" :key="index" :label="item.factoryName"
                     :value="item.factoryId"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="部门" prop="deptId">
        <div style="display: flex">
          <treeselect style="width: 240px;" v-model="queryParams.deptId" :options="deptOptions" :show-count="true"
                      placeholder="请选择部门"/>
        </div>
      </el-form-item>
      <el-form-item label="包含下级" prop="deptId">
        <div style="display: flex">
          <el-checkbox style="margin-left: 20px" v-model="queryParams.checked"></el-checkbox>
        </div>
      </el-form-item>
      <el-form-item label="申请人" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入申请人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="隐患区域" prop="riskArea">
        <el-select v-model="queryParams.riskArea" placeholder="请选择隐患区域" clearable>
          <el-option
            v-for="dict in dict.type.work_risk_area"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="隐患类别" prop="riskType">
        <el-select v-model="queryParams.riskType" placeholder="请选择隐患类别" clearable>
          <el-option
            v-for="dict in dict.type.work_risk_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="隐患级别" prop="riskLevel">
        <el-select v-model="queryParams.riskLevel" placeholder="请选择隐患级别" clearable>
          <el-option
            v-for="dict in dict.type.work_risk_level"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="隐患级别" prop="riskLevel">
        <el-select v-model="queryParams.riskLevel" placeholder="请选择隐患级别" clearable>
          <el-option
            v-for="dict in dict.type.work_risk_level"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="防范措施" prop="measure">
        <el-select v-model="queryParams.measure" placeholder="请选择防范措施" clearable>
          <el-option
            v-for="dict in dict.type.work_risk_measure"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['work:risk:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['work:risk:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['work:risk:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['work:risk:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="riskList" @selection-change="handleSelectionChange">
      <el-table-column fixed type="selection" width="55" align="center"/>
      <el-table-column label="编号" align="center" prop="riskId"/>
      <el-table-column label="厂区名称" align="center" prop="factoryName"/>
      <el-table-column label="部门名称" align="center" prop="deptName"/>
      <el-table-column label="申请人" align="center" prop="userName"/>
      <el-table-column label="隐患区域" align="center" prop="riskArea">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.work_risk_area" :value="scope.row.riskArea"/>
        </template>
      </el-table-column>
      <el-table-column label="隐患类别" align="center" prop="riskType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.work_risk_type" :value="scope.row.riskType"/>
        </template>
      </el-table-column>
      <el-table-column label="隐患级别" align="center" prop="riskLevel">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.work_risk_level" :value="scope.row.riskLevel"/>
        </template>
      </el-table-column>
      <el-table-column label="防范措施" align="center" prop="measure">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.work_risk_measure" :value="scope.row.measure"/>
        </template>
      </el-table-column>
      <el-table-column label="上传图片" align="center" prop="image" width="100">
        <template slot-scope="scope">
          <image-preview v-if="scope.row.image" :src="scope.row.image" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="整改照片" align="center" prop="photo" width="100">
        <template slot-scope="scope">
          <image-preview v-if="scope.row.photo" :src="scope.row.photo" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column
        label="审批进度"
        align="center" prop="schedule">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.common_schedule" :value="scope.row.schedule"/>
        </template>
      </el-table-column>
      <el-table-column label="备注说明" align="center" prop="remark"/>
      <el-table-column label="完成时间" align="center" prop="completionTime" width="120">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.completionTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="120">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right">
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['work:risk:view']"
            icon="el-icon-reading"
            size="mini"
            type="text"
            @click="preView(scope.row)"
          >查看
          </el-button>

          <el-button
            v-if="scope.row.schedule === 'unapproved'"
            v-hasPermi="['work:risk:submit']"
            icon="el-icon-position"
            size="mini"
            type="text"
            @click="submit(scope.row)"
          >隐患提交
          </el-button>

          <el-button
            v-if="scope.row.schedule === 'running'"
            v-hasPermi="['work:risk:revoke']"
            icon="el-icon-bottom"
            size="mini"
            type="text"
            @click="revoke(scope.row)"
          >隐患撤销
          </el-button>

          <el-button
            v-if="scope.row.schedule === 'terminated' || scope.row.schedule === 'canceled'"
            v-hasPermi="['work:risk:resubmit']"
            icon="el-icon-position"
            size="mini"
            type="text"
            @click="resubmit(scope.row)"
          >重新提交
          </el-button>

          <el-dropdown v-hasPermi="['work:risk:edit','work:risk:remove']"
                       v-if=" scope.row.schedule === 'unapproved'
                       || scope.row.schedule === 'terminated'
                       || scope.row.schedule === 'canceled'
                       || checkRole(['admin'])"
          >
              <span class="el-dropdown-link">
                更多<i class="el-icon-arrow-down el-icon--right"></i>
              </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item v-hasPermi="['work:risk:edit']">
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-edit"
                  @click="handleUpdate(scope.row)"
                >修改
                </el-button>
              </el-dropdown-item>
              <el-dropdown-item v-hasPermi="['work:risk:remove']">
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-delete"
                  @click="handleDelete(scope.row)"
                >删除
                </el-button>
              </el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <SelectFlow ref="selectFlow"></SelectFlow>
  </div>
</template>

<script>
import {delRisk, getRisk, listRisk, reapply, revoke, submit,} from "@/api/work/risk";
import {deptTreeSelect} from "@/api/system/user";
import Treeselect from '@riophae/vue-treeselect'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'
import {checkRole} from '@/utils/permission'
import {listFactoryVo} from "@/api/system/factory";

export default {
  name: "Risk",
  dicts: ['work_risk_level', 'work_risk_area', 'work_risk_type', 'common_schedule','work_risk_measure'],
  components: {Treeselect},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 隐患上报表格数据
      riskList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        checked: false,
        deptId: null,
        userId: null,
        factoryId: null,
        deptName: null,
        userName: null,
        riskArea: null,
        riskType: null,
        riskLevel: null,
        measure: null,
        schedule: null,
      },

      // 部门树选项
      deptOptions: [],

      factoryList: [],
    };
  },
  created() {
    this.getDeptTree();
    this.listFactoryVoFun();
    this.getList();
  },
  methods: {
    checkRole,
    /**
     * 查询厂区列表
     */
    listFactoryVoFun() {
      listFactoryVo().then((res) => {
        this.factoryList = res.data
      })
    },
    /** 查询部门下拉树结构 */
    getDeptTree() {
      deptTreeSelect().then(response => {
        this.deptOptions = response.data
      })
    },
    // 预览
    preView(row) {
      if (row.schedule === 'running' || row.schedule === 'completed' || row.schedule === 'terminated') {
        this.$tab.openPage("查询隐患详情", "/work/risk/viewRisk/index", {
          procInsId: row.processId,
          formType: '1',
          businessId: row.riskId,
        })
      } else {
        this.$tab.openPage("查询隐患详情", "/work/risk/viewRisk/index", {
          formType: '1',
          businessId: row.riskId,
          definitionId: row.definitionId,
        })
      }
    },
    // 提交审核
    submit(row) {
      this.$confirm('确定提交审核吗?', '温馨提示！', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const riskId = row.riskId
        submit(riskId).then(response => {
          this.$modal.msgSuccess('提交成功')
          this.getList()
        })
      }).catch(() => {
      })
    },
    // 撤销提交
    revoke(row) {
      this.$confirm('确定撤销提交审核吗?', '温馨提示！', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const riskId = row.riskId
        revoke(riskId).then(response => {
          this.$modal.msgSuccess('撤销成功')
          this.getList()
        })
      }).catch(() => {
      })
    },
    // 重新提交
    resubmit(row) {
      this.$confirm('确定重新提交审核吗?', '温馨提示！', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const riskId = row.riskId
        reapply(riskId).then(response => {
          this.$modal.msgSuccess('重新提交成功')
          this.getList()
        })
      }).catch(() => {
      })
    },


    /** 查询隐患上报列表 */
    getList() {
      this.loading = true;
      listRisk(this.queryParams).then(response => {
        this.riskList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.riskId)

      if (!this.checkRole(['admin'])) {
        for (let i = 0; i < selection.length; i++) {
          if (selection[i].schedule === 'running' || selection[i].schedule === 'completed') {
            this.single = true
            this.multiple = true
            break
          } else {
            this.single = selection.length !== 1
            this.multiple = !selection.length
          }
        }
      } else {
        this.single = selection.length !== 1
        this.multiple = !selection.length
      }
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.$refs.selectFlow.getFlow(this.$flowMenu.riskFlowMenu)
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      const riskId = row.riskId || this.ids
      getRisk(riskId).then(response => {
        let data = response.data
        this.$tab.openPage("修改隐患", "/work/risk/updateRisk/index", {
          definitionId: data.definitionId,
          processName: data.processName,
          businessId: data.riskId,
        })
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const riskIds = row.riskId || this.ids;
      this.$modal.confirm('是否确认删除隐患上报编号为"' + riskIds + '"的数据项？').then(function () {
        return delRisk(riskIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('work/risk/export', {
        ...this.queryParams
      }, `risk_${new Date().getTime()}.xlsx`)
    }
  },

  activated() {
    this.getList();
  }
};
</script>

<style lang="scss" scoped>
.el-dropdown-link {
  cursor: pointer;
  color: var(--current-color);
}

.el-icon-arrow-down {
  font-size: 12px;
}
</style>
